var http = require('http')
var fs = require('fs')
var app = http.createServer()
app.on('request', function (request, response) {
  console.log('收到客户端请求，请求路径是：' + request.url)
  var url = request.url
  var pathname = url.pathname
  if (url === '/') {
    fs.readFile('index.html', function (err, data) {
      if (err) {
        return response.end('404 Not Found')
      }
      response.end(data)
    })
  }
  else if (url.indexOf('/node_modules/') === 0) {
    fs.readFile('.' + pathname, function (err, data) {
      if (err) {
        return response.end('404 Not Found')
      }
      response.end(data)
    })
  }
})
app.listen(3000, function () {
  console.log('服务器启动成功了，可以通过http://127.0.0.1:3000/ 进行访问')
})
const io = require('socket.io')(app)
io.on('connection', socket => {
  console.log('有用户连接了')
  socket.on('haha', data => {
    console.log(data)
    socket.emit('send', data)
  })
})